package com.baomidou.mybatisplus.core.conditions.query;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Entity 对象封装操作类</p>
 *
 * <p>项目名称：fakerMybatisPlusUtil</p>
 *
 * <p>注释:无</p>
 *
 * <p>Copyright: Copyright Faker(c) 2023/1/20</p>
 *
 * <p>公司: Faker</p>
 *
 * @author 淡梦如烟
 * @version 1.0
 * @date 2023/1/20 20:36
 */
public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>> implements IQuery<QueryWrapper<T>, T, String> {

    /**
     * 查询字段
     */
    private SharedString sqlSelect = new SharedString();

    public QueryWrapper() {
        this(null);
    }

    public QueryWrapper(T entity) {
        super.setEntity(entity);
        super.initNeed();
    }

    public QueryWrapper(T entity, String... columns) {
        super.setEntity(entity);
        super.initNeed();
        this.select(columns);
    }

    /**
     * 非对外公开的构造方法,只用于生产嵌套 sql
     *
     * @param entityClass 本不应该需要的
     */
    private QueryWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
                         SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        super.setEntityClass(entityClass);
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    public QueryWrapper<T> select(String... columns) {
        if (ArrayUtils.isNotEmpty(columns)) {
            this.sqlSelect.setStringValue(join(StringPool.COMMA, columns));
        }
        return typedThis;
    }

    /**
     * join
     *
     * @param comma
     * @param columns
     * @return
     */
    private String join(String comma, String[] columns) {
        if (columns == null || columns.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (String column : columns) {
            if (null != column) {
                sb.append(column).append(comma);
            }
        }
        if (sb.length() > 0) {
            sb.setLength(sb.length() - comma.length());
        }
        return sb.toString();
    }

//    @Override
//    public QueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
//        super.setEntityClass(entityClass);
//        this.sqlSelect.setStringValue(TableInfoHelper.getTableInfo(getEntityClass()).chooseSelect(predicate));
//        return typedThis;
//    }

    /**
     * 实体对象（子类实现）
     *
     * @return 泛型 T
     */
    @Override
    public T getEntity() {
        return null;
    }

    @Override
    public String getSqlSelect() {
        return sqlSelect.getStringValue();
    }

//    /**
//     * 返回一个支持 lambda 函数写法的 wrapper
//     */
//    public LambdaQueryWrapper<T> lambda() {
//        return new LambdaQueryWrapper<>(getEntity(), getEntityClass(), sqlSelect, paramNameSeq, paramNameValuePairs,
//                expression, lastSql, sqlComment, sqlFirst);
//    }

    /**
     * 用于生成嵌套 sql
     * <p>
     * 故 sqlSelect 不向下传递
     * </p>
     */
    @Override
    protected QueryWrapper<T> instance() {
        return new QueryWrapper<T>(getEntity(), getEntityClass(), paramNameSeq, paramNameValuePairs, new MergeSegments(),
                SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSelect.toNull();
    }

    @Override
    public String toString() {
        return "QueryWrapper{" +
                " paramNameSeq=" + paramNameSeq +
                ", paramNameValuePairs=" + paramNameValuePairs +
                ", lastSql=" + lastSql +
                ", sqlComment=" + sqlComment +
                ", sqlFirst=" + sqlFirst +
                ", expression=" + expression +
                ", sqlSelect=" + sqlSelect +
                '}';
    }
}

